package org.theonept.travian.t4a.report;

import java.util.List;

import org.theonept.travian.model.Compte;
import org.theonept.travian.model.Rapport;
import org.theonept.travian.t4a.AbstractFragment;
import org.theonept.travian.t4a.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * A dummy fragment representing a section of the app, but that simply displays
 * dummy text.
 */
public class ReportSectionFragment extends AbstractFragment implements AdapterView.OnItemClickListener {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	public static final String ARG_SECTION_NUMBER = "section_number";
	private ListView reportListView;
	private ListReportAdapter listReportAdapter;
	private Compte compte;

	public ReportSectionFragment() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_compte_report, container, false);
		reportListView = (ListView) rootView.findViewById(R.id.listReport);
		List<Rapport> rapports = (List<Rapport>) getArguments().getSerializable("reports");
		if (listReportAdapter == null) {
			listReportAdapter = new ListReportAdapter(getActivity());
		}
		this.compte = (Compte) getArguments().getSerializable("compte");
		reportListView.setAdapter(listReportAdapter);
		if (rapports != null) {
			setData(rapports);
		}
		reportListView.setOnItemClickListener(this);
		setHasOptionsMenu(true);

		return rootView;
	}

	public void setData(List<Rapport> rapports) {
		listReportAdapter.setData(rapports);

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		Rapport rapport = listReportAdapter.getItem(position);
		Intent intentRapport = new Intent(getActivity(), RapportActivity.class);
		intentRapport.putExtra("rapport", rapport);
		intentRapport.putExtra("compte", compte);
		startActivity(intentRapport);
	}
}